package com.atguigu.ai.controller;

import lombok.RequiredArgsConstructor;
import org.springframework.ai.image.Image;
import org.springframework.ai.image.ImagePrompt;
import org.springframework.ai.image.ImageResponse;
import org.springframework.ai.openai.OpenAiImageModel;
import org.springframework.ai.openai.OpenAiImageOptions;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author Jungle
 * @create 2025-03-01 14:07
 */
@RestController
@RequestMapping("/openai")
@RequiredArgsConstructor
public class OpenAIImageController {
    private final OpenAiImageModel openaiImageModel;

    // @GetMapping(value = "/ai/generateImage", produces = "text/html")
    @GetMapping(value = "/ai/generateImage")
    public String generateImage(@RequestParam String message) {
        ImageResponse response = openaiImageModel.call(
                new ImagePrompt(message, OpenAiImageOptions.builder()
                        // hd 高质量
                        .quality("standard")
                        // 生成 4 张图像
                        .N(1)
                        // .height(1024)
                        // .width(1024)
                        .build())
        );
        Image output = response.getResult().getOutput();
        //     获取url
        String url = output.getUrl();
        //     获取base64编码
        String b64Json = output.getB64Json();

        return url;

    }
}
